<?php
/*
-- OVERVIEW
Paging class provides paging 'allin one'
 directly uses REQUEST, SESSION and SQL (Builds LIMIT into query) and UI display element

-- RECCOMEND:
Break into seperate classes
-- PagingBase: Track and store paging status for other Paging classes.  -->PagingPersist, 
-- PagingForm_x:(Basic, etc) Provide UI. -->PagingBase
-- PagingFilter: Provide filter addition based on storate to data retrival.  -->PagingBase 


*/


class CF_Paging {

	var $thetable;
	var $table_name;
	/**
	* @var string $selfpage The name of the page, usualy set by $_SERVER['SELF']
	*/
	var $selfpage;
//paging variables
	var $pageform;
	var $startat;
	var $numrows;
	var $post_rowlimit;
	var $startatvar = 'startat';
	var $numrowsvar = 'numrows';
	var $default_row_limit = 10;
	var $nextpagestart;
	var $prevpagestart;
	var $rowsmax = NULL; // default Null for unknown
	//var $oracle_post_rowlimit;
	//var $oracle_pre_rowlimit = "SELECT * FROM ( SELECT thequery.*, rownum rnum from ( ";
	//var $pgsql_post_rowlimit;
	//var $fbird_fix_rowlimit;	

	
	
	/**
	 *  make_pageform  Method will produce the "First, Previous <-- --> Next " line for
     * naviating paginated records
     *
	 */
	function make_pageform(){
			return $this->pageform;
	}	
	
	function make_query_post_rowlimit(){
		return $this->post_rowlimit();
	
	}


	/**
	 *  get_pagination  Method will recoeve pagination from $_REQUEST and $_SESSION
     * variables and set internal variables for use.  Only call if pagination is used
	 */
	function get_pagination(){
	
		//paging
		if (!empty($_REQUEST[$this->numrowsvar])){
			$this->numrows =  $_REQUEST[$this->numrowsvar];
		}
		else if(isset($_SESSION[$this->table_name . '_numrows'])){
			$this->numrows =  $_SESSION[$this->table_name . '_numrows'];
		}
		else if ($this->default_row_limit != 0) {
			$this->numrows = $this->default_row_limit;
			}
		
		if (isset($_REQUEST[$this->startatvar])){
			$this->startat =  (int)$_REQUEST[$this->startatvar];
			$this->post_rowlimit = " LIMIT $this->startat , $this->numrows ";
			//$this->oracle_post_rowlimit = " ) thequery <= " . $this->startat + $this->numrows;
			//$this->oracle_post_rowlimit .= " ) where rnum >= $this->startat ORDER BY rnum ";
			//$this->pgsql_post_rowlimit = " LIMIT $this->numrows OFFSET $this->startat ";
			//$this->fbird_fix_rowlimit = " FIRST $this->numrows SKIP $this->startat ";
			
			
		//print "JUST SET REQUEST startat:	$this->startat <br>";	
		}		
		else if(isset($_SESSION[$this->table_name . '_startat'])){
			$this->startat =  $_SESSION[$this->table_name . '_startat'];
			$this->post_rowlimit = " LIMIT $this->startat , $this->numrows ";
		//print "JUST SET SESSION startat:	$this->startat <br>";			
		}else if ($_REQUEST[$this->startatvar] == 'RESET'){ // NOT WORKING
           unset( $_SESSION[$this->table_name . '_startat']);
           unset($_SESSION[$this->table_name . '_numrows']);
           $this->startat = 0;
           $this->numrows = 'MAX';
            unset($this->post_rowlimit);
         }
		else{
			$this->startat = 0;
			$this->post_rowlimit = " LIMIT $this->startat , $this->numrows ";
		}
		

		if(isset($this->post_rowlimit)){
		$_SESSION[$this->table_name . '_numrows'] = $this->numrows;
		$_SESSION[$this->table_name . '_startat'] = $this->startat;
		}
		
//$this->rowsmax = count($array_list);
			//paging calculations
		if(is_null($this->rowsmax)){
			$this->nextpagestart = $this->startat + $this->numrows;
		}
		else{

			if($this->startat + $this->numrows < $this->rowsmax){
				$this->nextpagestart = $this->startat + $this->numrows;
			}
			else{
			$this->nextpagestart = $this->startat;
			}
		}
		
		if($this->startat - $this->numrows > 0){
			$this->prevpagestart = $this->startat - $this->numrows;
			}
		else{
			$this->prevpagestart = 0;
		}
		$this->pageform = "<a href='" . $_SERVER['PHP_SELF'] . "?$this->startatvar=0&$this->numrowsvar=$this->numrows'>First</a>\n";
		$this->pageform .= "<a href='" . $_SERVER['PHP_SELF'] . "?$this->startatvar=$this->prevpagestart&$this->numrowsvar=$this->numrows'>Previous</a>\n";
		$this->pageform .= "<--   -->";
		$this->pageform .= "<a href='" . $_SERVER['PHP_SELF'] . "?$this->startatvar=$this->nextpagestart&$this->numrowsvar=$this->numrows'>Next</a>\n";
		if(!empty($this->rowsmax)){
            // $lastpage = (int)($this->rowsmax / $this->numrows);
            $lastpagestart = $this->rowsmax - $this->numrows;
            if($lastpagestart < 0){
                $lastpagestart = 0;
                }
         $this->pageform .= "<a href='" . $_SERVER['PHP_SELF'] . "?$this->startatvar=$lastpagestart&$this->numrowsvar=$this->numrows'>Last</a>\n"; 
            }
        //print "$this->prevpagestart : $this->nextpagestart : $this->rowsmax <br>";
		//end paging	
	
	}
	
	
}
?>